<?php
/**
 * 订单管理接口
 */
namespace app\api\controller;

use app\common\model\OrderModel;
use app\common\model\SystemMsg;
use app\common\controller\Tools;
use Symfony\Component\DomCrawler\Field\InputFormField;
use think\Controller;
use think\Db;
use think\Request;

class Order extends Common
{

    public function _initialize()
    {

    }


    /**
     * 修改续租状态
     * @return json_array
     */
    public function setReletStatus()
    {
        if (!session('merchant')) {
            return returnJson(0, '未登录');
        }
        $orderId = (int)input('order_id');
        $reletStatus = (int)input('relet_status');
        if ( !$orderId || (!in_array($reletStatus, [2,3])) ) return returnJson(0, '参数错误');

        $data['relet_status'] = $reletStatus;
        $templateCode = 'SMS_105950026';
        if (3 === $reletStatus) { //商家拒绝续租
            $data['order_refund_goods_status'] = 3;//退款中
            $templateCode = 'SMS_105800034';
        }

        $res = Db('goods_order')->where('order_id', $orderId)->update($data);
        if ($res) {
            $order = Db::name('goods_order')->alias('a')
                ->join('user b', 'a.order_user_id=b.user_id')
                ->where('a.order_id', $orderId)
                ->field('a.order_id, a.order_order_sn, a.order_merchant_id merchant_id, b.user_id, b.user_phone')
                ->find();

            //发站内信
            $order['title'] = "订单消息 - 您的续租订单{$order['order_order_sn']}已审核通过";
            $order['content'] = "您好！您的续租订单{$order['order_order_sn']}卖家已审核通过。";
            if (3 === $reletStatus) {
                $order['title'] = "订单消息 - 您的续租订单{$order['order_order_sn']}审核未通过";
                $order['content'] = "您好！您的续租订单{$order['order_order_sn']}审核未通过，请及时归还。";
            }
            SystemMsg::sendOrderMsg($order);

            // 发短信
            $paramString = "{'ordersn':'{$order['order_order_sn']}'}";
            $tool = new Tools();
            $tool->sendMessage($paramString, $order['user_phone'], '租介', $templateCode);

            return returnJson(1, '修改成功');
        }
        return returnJson(0, '未修改');
    }

    /**
     * 修改物流公司
    */
    public function editShipCompany(){
        if(Request::instance()->method() == 'POST'){
            if (!session('merchant')) {
                echo json_encode(array('status'=>0,'msg'=>"未登录"));
                exit();
            }
            $company_name = input("name/s","");
            $company_code = input("code/s","");
            $order_id = input("order_id/n",0);
            if($order_id == 0){
                echo json_encode(array('status'=>0,'msg'=>"订单号有误，物流公司保存失败"));
                exit();
            }
            $res = Db::name("goods_order_deliver")->where("order_id = $order_id")
                ->update(['order_deliver_company'=>$company_name,'order_logistics_name'=>$company_code]);
            if($res !== false){
                echo json_encode(array('status'=>1,'msg'=>"物流公司保存成功","data"=>['code'=>$company_code,'name'=>$company_name]));
            }else{
                echo json_encode(array('status'=>0,'msg'=>"物流公司保存失败"));
            }
            exit();
        }
    }

    /**
     * 修改物流单号
     */
    public function editShipExpressid(){
        if(Request::instance()->method() == 'POST'){
            if (!session('merchant')) {
                echo json_encode(array('status'=>0,'msg'=>"未登录"));
                exit();
            }
            $expressid = input("eid/s","");
            $order_id = input("order_id/n",0);
            if($order_id == 0){
                echo json_encode(array('status'=>0,'msg'=>"订单号有误，物流单号保存失败"));
                exit();
            }
            $res = Db::name("goods_order_deliver")->where("order_id = $order_id")
                ->update(['order_deliver_expressid'=>$expressid]);
            if($res !== false){
                echo json_encode(array('status'=>1,'msg'=>"物流单号保存成功","data"=>['eid'=>$expressid]));
            }else{
                echo json_encode(array('status'=>0,'msg'=>"物流单号保存失败"));
            }
            exit();
        }
    }


    // ======================================
    // ============  私有方法   =============
    // ======================================




}
